package com.hao;


import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.options.PDFOptions;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

/**
 * TODO
 *
 * @author 97472
 * @version 1.0
 * @date 2021/4/15 10:21
 */
public class jvppeteer {
    @Test
    public void testJvppeter(){
        //自动下载，第一次下载后不会再下载
        try {
            BrowserFetcher.downloadIfNotExist(null);
            ArrayList<String> argList = new ArrayList();
            LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withHeadless(false).build();
            argList.add("--no-sandbox");
            argList.add("--disable-setuid-sandbox");
            Browser browser = Puppeteer.launch(options);
            Page page = browser.newPage();
            page.goTo("https://mp.weixin.qq.com/s/uwSKCsslZuxvKdWxI1VUxg");
            Thread.sleep(5000);
            System.out.println(page.content());
            page.close();
            browser.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @Test
    public void testHtmlUnit(){
        WebClient wc = new WebClient(BrowserVersion.CHROME);
        //是否使用不安全的SSL
        wc.getOptions().setUseInsecureSSL(true);
        //启用JS解释器，默认为true
        wc.getOptions().setJavaScriptEnabled(true);
        //禁用CSS
        wc.getOptions().setCssEnabled(false);
        //js运行错误时，是否抛出异常
        wc.getOptions().setThrowExceptionOnScriptError(false);
        //状态码错误时，是否抛出异常
        wc.getOptions().setThrowExceptionOnFailingStatusCode(false);
        //是否允许使用ActiveX
        wc.getOptions().setActiveXNative(true);
        //等待js时间
        wc.waitForBackgroundJavaScript(600*1000);
        //设置Ajax异步处理控制器即启用Ajax支持
        wc.setAjaxController(new NicelyResynchronizingAjaxController());
        //设置超时时间
        wc.getOptions().setTimeout(1000000);
        wc.waitForBackgroundJavaScript(10000);

//        try {
//            HtmlPage page = wc.getPage("https://mp.weixin.qq.com/s/uwSKCsslZuxvKdWxI1VUxg");
//            Thread.sleep(10000);
//            System.out.println(page.asXml());
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        HtmlPage page = null;
        try {
            page = wc.getPage("https://mp.weixin.qq.com/s/uwSKCsslZuxvKdWxI1VUxg");
        } catch (Exception e) {
            System.out.println("Get page error");
        }
        int z = wc.waitForBackgroundJavaScript(10000);
//        int counter = 1000;
//        while (z > 0) {
//            counter += 1000;
//            z = wc.waitForBackgroundJavaScript(counter);
//            if (z == 0) {
//                break;
//            }
//            synchronized (page) {
//                System.out.println("wait");
//                try {
//                    page.wait(500);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
        System.out.println(page.asXml());
    }
}
